package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.Pl1Adapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.BookDetailInfoSubBean;
import com.shuorikeji.huohuo.bean.BookPlInfoBean;
import com.shuorikeji.huohuo.bean.BookPlMainBean;
import com.shuorikeji.huohuo.bean.StarTagBean;
import com.shuorikeji.huohuo.bean.UserInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.GsonUtil;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookPlActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_pf)
    TextView tvPf;
    @BindView(R.id.tv_pl_count)
    TextView tvPlCount;
    @BindView(R.id.rb_pl_star)
    RatingBar rbPlStar;
    @BindView(R.id.tv_go_to_read)
    TextView tvGoToRead;
    @BindView(R.id.ll_tag_type)
    LinearLayout llTagType;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private List<StarTagBean> tagList = new ArrayList<>();
    private int curPage;
    private int pageSize;
    private Pl1Adapter pl1Adapter;
    private List<BookPlInfoBean> plList = new ArrayList<>();
    private BookDetailInfoSubBean bookInfoDta;
    private int PL_COUNT;//评论数
    private int plFlag, isNew, starInt;//获取评论数据传参
    private UserInfoBean userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_pl);
    }

    @Override
    protected void initData() {
        userInfo = BusinessUtils.getUserInfo();
        initSmartRefreshLayout();
        initAdapter();
        initClick();
        setStarTagData();
        initIntent();
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(BookPlActivity.this, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 15, 0));
        pl1Adapter = new Pl1Adapter(BookPlActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 2:
                        BookPlInfoBean data = (BookPlInfoBean) object;
                        if (userInfo != null) {

                        }
                        break;
                }
            }
        });
        rvData.setAdapter(pl1Adapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        String bookInfoStr = intent.getStringExtra("BOOK_INFO");
        bookInfoDta = GsonUtil.parseJsonToBean(bookInfoStr, BookDetailInfoSubBean.class);
        PL_COUNT = intent.getIntExtra("PL_COUNT", -1);
        setBookInfo();
        curPage = 1;
        pageSize = 20;
        plFlag = 1;
        isNew = 0;
        starInt = -1;
        getBookPl();
    }

    private void setBookInfo() {
        if (bookInfoDta != null) {
            tvPf.setText(bookInfoDta.getScore());
            tvPlCount.setText(PL_COUNT + " comments");
            tvName.setText(bookInfoDta.getName());
            try {
                rbPlStar.setRating((Double.parseDouble(bookInfoDta.getScore()) >= 5) ? 5 : (int) Double.parseDouble(bookInfoDta.getScore()));
            } catch (Exception e) {
                e.printStackTrace();
                rbPlStar.setRating(0);
            }
        }
    }

    private void getBookPl() {
        mPresenter.getBookPl(BookPlActivity.this, plFlag, curPage, bookInfoDta.getId(), pageSize, isNew, starInt, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        BookPlMainBean data = (BookPlMainBean) object;
                        if (curPage == 1) {
                            plList.clear();
                        }
                        plList.addAll(data.getData().getData());
                        pl1Adapter.setData(plList);
                        break;
                    case 2:
                        srlLayout.finishLoadMore();
                        srlLayout.finishRefresh();
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookPlActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(BookPlActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getBookPl();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getBookPl();
            }
        });
    }

    private void setStarTagData() {
        tagList.clear();
        StarTagBean data = new StarTagBean();
        data.setTag("全部");
        data.setSec(true);
        tagList.add(data);
        data = null;

        StarTagBean data1 = new StarTagBean();
        data1.setTag("最新");
        data1.setSec(false);
        tagList.add(data1);
        data1 = null;

//        StarTagBean data8 = new StarTagBean();
//        data8.setTag("10 星");
//        data8.setSec(false);
//        tagList.add(data8);
//        data8 = null;
//
//        StarTagBean data9 = new StarTagBean();
//        data9.setTag("9 星");
//        data9.setSec(false);
//        tagList.add(data9);
//        data9 = null;
//
//        StarTagBean data10 = new StarTagBean();
//        data10.setTag("8 星");
//        data10.setSec(false);
//        tagList.add(data10);
//        data10 = null;
//
//        StarTagBean data11 = new StarTagBean();
//        data11.setTag("7 星");
//        data11.setSec(false);
//        tagList.add(data11);
//        data11 = null;
//
//        StarTagBean data12 = new StarTagBean();
//        data12.setTag("6 星");
//        data12.setSec(false);
//        tagList.add(data12);
//        data12 = null;

        StarTagBean data2 = new StarTagBean();
        data2.setTag("5 星");
        data2.setSec(false);
        tagList.add(data2);
        data2 = null;

        StarTagBean data3 = new StarTagBean();
        data3.setTag("4 星");
        data3.setSec(false);
        tagList.add(data3);
        data3 = null;

        StarTagBean data4 = new StarTagBean();
        data4.setTag("3 星");
        data4.setSec(false);
        tagList.add(data4);
        data4 = null;

        StarTagBean data5 = new StarTagBean();
        data5.setTag("2 星");
        data5.setSec(false);
        tagList.add(data5);
        data5 = null;

        StarTagBean data6 = new StarTagBean();
        data6.setTag("1 星");
        data6.setSec(false);
        tagList.add(data6);
        data6 = null;

        StarTagBean data7 = new StarTagBean();
        data7.setTag("0 星");
        data7.setSec(false);
        tagList.add(data7);
        data7 = null;

        showStarTagView();
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvGoToRead.setOnClickListener(view -> {
            if (bookInfoDta != null) {
                Intent intent = new Intent(BookPlActivity.this, BookMlActivity.class);
                intent.putExtra("BOOK_ID", bookInfoDta.getId());
                intent.putExtra("BOOK_INFO", JsonUtil.toJson(bookInfoDta));
                startActivity(intent);
            }
        });
    }

    /**
     * 热门标签view
     */
    private void showStarTagView() {
        llTagType.removeAllViews();
        for (int i = 0; i < tagList.size(); i++) {
            View subView = View.inflate(BookPlActivity.this, R.layout.hot_tag_item_layout, null);
            subView.setPadding(40, 0, 10, 0);
            LinearLayout llMain = subView.findViewById(R.id.ll_main);
            TextView tvTagName = subView.findViewById(R.id.tv_tag_name);
            tvTagName.setBackground((tagList.get(i).isSec()) ? getResources().getDrawable(R.drawable.shape_6_f9e141) :
                    getResources().getDrawable(R.drawable.shape_6_f2f2f2));
            tvTagName.setTextColor((tagList.get(i).isSec()) ? getResources().getColor(R.color.color_333333) :
                    getResources().getColor(R.color.color_666666));
            tvTagName.setText(tagList.get(i).getTag());
            llMain.setId(i);
            llTagType.addView(subView);
            llMain.setOnClickListener(view -> {
                for (int j = 0; j < tagList.size(); j++) {
                    if (llMain.getId() == j) {
                        tagList.get(j).setSec(true);
                    } else {
                        tagList.get(j).setSec(false);
                    }
                }
                showStarTagView();
                if ("全部".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 1;
                    isNew = 0;
                    starInt = -1;
                } else if ("最新".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 1;
                    isNew = 1;
                    starInt = -1;
                } else if ("10 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 10;
                } else if ("9 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 9;
                } else if ("8 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 8;
                } else if ("7 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 7;
                } else if ("6 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 6;
                } else if ("5 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 5;
                } else if ("4 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 4;
                } else if ("3 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 3;
                } else if ("2 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 2;
                } else if ("1 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 1;
                } else if ("0 星".equals(tagList.get(llMain.getId()).getTag())) {
                    plFlag = 2;
                    isNew = 1;
                    starInt = 0;
                }
                curPage = 1;
                getBookPl();
            });
        }
    }
}